﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Angel.Daos.IManager;
using Angel.Entities;

namespace Angel.Daos.Manager
{
    public class Group_UserManager : IGroup_UserManager
    {
        AngelDataContext ctx = new AngelDataContext();
        GroupManager groupManager = new GroupManager();

        public IEnumerable<Group_User> GetAll()
        {
            return ctx.Group_Users;
        }

        public Group_User GetByID(int id)
        {
            return ctx.Group_Users.SingleOrDefault(gu => gu.ID == id);
        }

        public void Add(Group_User group)
        {
            throw new NotImplementedException();
        }

        public void Delete(Group_User groupUser)
        {
            ctx.Group_Users.Attach(groupUser);
            ctx.Group_Users.DeleteOnSubmit(groupUser);
            ctx.SubmitChanges();
        }

        public void Delete(int id)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<Group> GetGroupByUserID(int userId)
        {
            IEnumerable<int> listId = ctx.Group_Users.Where(q => q.UserID == userId).Select(q => q.GroupID);
            List<Group> listGroup = new List<Group>();
            foreach (int i in listId)
            {
                listGroup.Add(groupManager.GetByID(i));
            }
            return listGroup;
        }
    }
}
